package com.dog.basics.netty.business.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.dog.basics.common.util.RedisUtil;
import com.dog.basics.netty.business.IBusinessServer;
import com.dog.basics.netty.protobuf.dto.DogNewsContentDto;
import com.dog.basics.netty.util.SessionSocketHolder;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.log4j.Log4j2;

/**
 * @description: 远程控制自定义
 * @author: He
 * @date: Created in 2020/10/9 17:24
 * @version: 1.0
 * @modified By:
 */
@Log4j2
public class ResponseHandlerImplRemoteCustomControl implements IBusinessServer<DogNewsContentDto.Message> {

    /**
     * 远程控制自定义 - 执行
     *
     * @param ctx
     * @param msg
     * @throws InterruptedException guonh 2020年10月9日
     */
    @Override
    public void businessHandler(ChannelHandlerContext ctx, DogNewsContentDto.Message msg) throws InterruptedException {
        log.info("远程控制自定义回应消息:{}", msg.toString());
        // 查询设备标识
        Long dvId = SessionSocketHolder.getSession(ctx.channel().id().asLongText());
        // 声明redis 中的存储key
        String receivedKey = StrUtil.format("SEND-COMMAND:STATE:{}:{}",dvId,msg.getMsgType());
        // 存储应答结果
        RedisUtil.set(receivedKey, Validator.equal(msg.getMsgCode(), 1) ? Boolean.TRUE : Boolean.FALSE, 45);
    }
}